package string;

public class MyString {
    private char[] value;

    public static void main(String args[]) {
        MyString string1 = new MyString("asDfG  hjK  lm");
        System.out.println(string1);
        System.out.println(string1.length());
        System.out.println(string1.isEmpty());
        System.out.println(string1.charAt(4));
        System.out.println(string1.substring(1, 5));
        System.out.println(string1.concat(new MyString("concat-string")));
        System.out.println(string1.trim());
        System.out.println(string1.toLowerCase());
        System.out.println(string1.toUpperCase());
        System.out.println(string1.startsWith(new MyString("asD")));
        System.out.println(string1.endsWith(new MyString("lm")));
        System.out.println(string1.parseInt(new MyString("5654862")));
        System.out.println(string1.toBinary(0xFFFF));
        System.out.println(string1.indexOf(new MyString("DfG")));
    }

    public MyString() {
        this.value = new char[0];
    }

    public MyString(String s) {
        this.value = new char[s.length()];
        for (int i = 0; i < this.value.length; i++) {
            this.value[i] = s.charAt(i);
        }
    }

    public MyString(char[] value) {
        this(new String(value));
    }

    public MyString(MyString s) {
        this(s.value);
    }

    public int length() {
        return this.value.length;
    }

    public boolean isEmpty() {
        return value.length == 0;
    }

    public String toString() {
        return new String(this.value);
    }

    public char charAt(int i) {
        if (i >= 0 && i < this.value.length)
            return this.value[i];
        throw new StringIndexOutOfBoundsException(i);
    }

    //返回索引从begin至end-1的子串; 所以子串长度为 end - begin
    public MyString substring(int begin, int end) {
        int length = end - begin;
        if (begin >= 0 && length >= 0 && length <= this.value.length) {
            char[] subStringValue = new char[length];
            for (int i = 0; i < length; i++) {
                subStringValue[i] = this.value[begin + i];
            }
            return new MyString(subStringValue);
        } else {
            throw new StringIndexOutOfBoundsException();
        }
    }

    //返回序号从begin至串尾的子串
    public MyString substring(int begin) {
        return substring(begin, this.value.length);
    }

    //返回this与s串连接生成的串。若s==null，插入"null"
    public MyString concat(MyString s) {
        if (s == null)
            s = new MyString("null");
        char[] buffer = new char[this.value.length + s.length()];
        int i;
        for (i = 0; i < this.value.length; i++)
            buffer[i] = this.value[i];
        for (int j = 0; j < s.value.length; j++)
            buffer[i + j] = s.value[j];
        return new MyString(buffer);
    }

    //返回删除所有空格后的字符串
    public MyString trim() {
        char temp[] = new char[this.value.length];
        int j = 0;
        for (int i = 0; i < this.value.length; i++)
            if (this.value[i] != ' ')
                temp[j++] = this.value[i];
        return new MyString(temp).substring(0, j);
    }

    //将串中的大写字母全部转换成对应的小写字母
    public MyString toLowerCase() {
        char temp[] = new char[this.value.length];
        for (int i = 0; i < this.value.length; i++) {
            if (this.value[i] >= 'A' && this.value[i] <= 'Z') {
                temp[i] = (char) (this.value[i] - ('A' - 'a'));
            } else {
                temp[i] = this.value[i];
            }
        }

        return new MyString(temp);
    }

    //将串中的小写字母全部转换成对应的大写字母
    public MyString toUpperCase() {
        char temp[] = new char[this.value.length];
        for (int i = 0; i < this.value.length; i++) {
            if (this.value[i] >= 'a' && this.value[i] <= 'z') {
                temp[i] = (char) (this.value[i] + ('A' - 'a'));
            } else {
                temp[i] = this.value[i];
            }
        }
        return new MyString(temp);
    }

    //判断prefix是否前缀子串
    public boolean startsWith(MyString prefix) {
        return false;
    }

    //判断suffix是否后缀子串
    public boolean endsWith(MyString suffix) {
        return false;
    }

    public int parseInt(MyString str) {
        int value = 0;
        int radix = 10;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            value = value * radix + (ch - '0');
        }
        return value;
    }

    public MyString toBinary(int value) {
        char[] buffer = new char[32];
        for (int i = buffer.length - 1; i >= 0; i--) {
            buffer[i] = (char)((value & 1) + '0');
            value >>>= 1;
        }
        return new MyString(buffer);
    }
    //Brute-Force算法,返回当前串（目标串）中首个与模式串pattern匹配的子串序号，匹配失败时返回-1
    public int indexOf(MyString pattern) {
        int n = this.length(), m = pattern.length();
        if (n == 0 || n < m)
            return -1;
        int i = 0, j = 0;     //i、j分别为目标串和模式串当前字符下标
        while (i < n && j < m) {
            if (this.charAt(i) == pattern.charAt(j)) {
                System.out.println("t" + i + "=p" + j + "，");
                i++;
                j++;
            } else {
                System.out.println("t" + i + "!=p" + j + "，");
                i = i - j + 1;   //目标串下标i，退回到下个匹配子串序号
                j = 0;           //模式串下标j，退回到0
                if (n - i < m)   //若目标串剩余子串的长度<m，不再比较 n-i<m
                    break;
            }
        }
        if (j == m)              //匹配成功
            return i - j;        //返回匹配的子串序号
        return -1;               //匹配失败时返回-1
    }

}